/**
 * @description:
 * @author: qintao
 * @create: 2019-08-27 15:17
 **/
package com.guoranhao.web.controller.base;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.guoranhao.business.api.service.BaseInfoApi;
import com.guoranhao.business.model.*;
import com.guoranhao.business.param.EsSkuParam;
import com.guoranhao.business.vo.AppFunctionIconVo;
import com.guoranhao.business.vo.EsSpuVo;
import com.guoranhao.business.vo.PostVo;
import com.guoranhao.tool.constants.BaseErrorCodeEnum;
import com.guoranhao.tool.enums.ZeroOrOneEnum;
import com.guoranhao.tool.model.BasePage;
import com.guoranhao.tool.utils.LogUtil;
import com.guoranhao.tool.utils.StringUtil;
import com.guoranhao.tool.vo.MessageVo;
import com.guoranhao.web.util.SplicePathUtil;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Description:  基础控制器<br/>
 * Version V1.0  2019-8-30 16:59  by 秦涛   (qintao@cloud-young.com) 创建
 */
@RestController
@RequestMapping("/base/")
public class BaseController {

    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Reference(version = "1.0.1",url = "dubbo://127.0.0.1:20881")
    private BaseInfoApi baseInfoApi;


    /**
     * Description:  查询轮播图列表<br/>
     * Version V1.0  2019-8-27 15:41  by 秦涛   (qintao@cloud-young.com) 创建
     * @param page 分页
     * @return 列表
     */
    @RequestMapping(value = "getSlideConfig",method={ RequestMethod.GET,RequestMethod.POST})
    public MessageVo<List<SlideConfig>> getSlideConfig(@RequestBody BasePage page){
        try {
            if (Objects.isNull(page)){
                page = new BasePage();
                page.setPageNo(ZeroOrOneEnum.ONE.getCode());
                page.setPageSize(10);
            }
            MessageVo<List<SlideConfig>> result = baseInfoApi.getSlideConfig(page);
            if (result.getResult()){
                if (CollectionUtils.isNotEmpty(result.getData())){
                    result.getData().forEach(slideConfig -> slideConfig.setSlideImgUrl(SplicePathUtil.spliceUrl(slideConfig.getSlideImgUrl())));
                }
                return result;
            }
        }catch (Exception e){
            LogUtil.error(logger,e,"getSlideConfig_method_error,page:{0}", JSON.toJSONString(page));
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100008.getCode(),BaseErrorCodeEnum.ERRCODE_100008.getMsg());
    }

    /**
     * Description:  查询功能图标<br/>
     * Version V1.0  2019-8-27 17:14  by 秦涛   (qintao@cloud-young.com) 创建
     * @param categoryId 类型
     * @return 功能图标
     */
    @RequestMapping(value = "getAppFunctionIconList",method={ RequestMethod.GET})
    public MessageVo<List<AppFunctionIcon>> getAppFunctionIconList(Long categoryId ){
        try {
            if (Objects.isNull(categoryId)){
                return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100004.getCode(),BaseErrorCodeEnum.ERRCODE_100004.getMsg());
            }
            MessageVo<List<AppFunctionIcon>> result = baseInfoApi.getAppFunctionIconList(categoryId);
            if (result.getResult()){
                if (CollectionUtils.isNotEmpty(result.getData())){
                    result.getData().forEach(appFunctionIcon -> appFunctionIcon.setFunctionIconUrl(SplicePathUtil.spliceUrl(appFunctionIcon.getFunctionIconUrl())));
                }
                return result;
            }
        }catch (Exception e){
            LogUtil.error(logger,e,"getAppFunctionIconList_method_error,categoryId:{0}", categoryId);
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100009.getCode(),BaseErrorCodeEnum.ERRCODE_100009.getMsg());
    }

    /**
     * Description:  查询帖子文章列表<br/>
     * Version V1.0  2019-8-28 10:17  by 秦涛   (qintao@cloud-young.com) 创建
     * @param page 分页参数
     * @return 列表数据
     */
    @RequestMapping(value = "getPostList",method={ RequestMethod.GET,RequestMethod.POST})
    public MessageVo<List<PostVo>> getPostList(@RequestBody BasePage page){
        try {
            if (Objects.isNull(page)){
                page = new BasePage();
                page.setPageNo(ZeroOrOneEnum.ONE.getCode());
                page.setPageSize(10);
            }
            MessageVo<List<Post>> result = baseInfoApi.getPostList(page);
            if (result.getResult()){
                List<PostVo> postVos = result.getData().stream().map(PostVo::toVo).collect(Collectors.toList());
                return new MessageVo<>(true,postVos);
            }
        }catch (Exception e){
            LogUtil.error(logger,e,"getPostList_method_error,page:{0}", JSON.toJSONString(page));
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100010.getCode(),BaseErrorCodeEnum.ERRCODE_100010.getMsg());
    }




    /**
     * Description:  查询商品分类列表<br/>
     * Version V1.0  2019-8-28 15:44  by 秦涛   (qintao@cloud-young.com) 创建
     * @return 列表
     */
    @RequestMapping(value = "getEsCategoryList",method={ RequestMethod.GET,RequestMethod.POST})
    public MessageVo<List<EsCategory>> getEsCategoryList(){
        try {
            return baseInfoApi.getEsCategoryList();
        }catch (Exception e){
            LogUtil.error(logger,e,"getEsCategoryList_method_error");
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100011.getCode(),BaseErrorCodeEnum.ERRCODE_100011.getMsg());
    }

    /**
     * Description:  查询购物车数量<br/>
     * Version V1.0  2019-8-29 11:08  by 秦涛   (qintao@cloud-young.com) 创建
     * @param openId 用户id
     * @return 数量
     */
    @RequestMapping(value = "getShoppingCartCount",method={ RequestMethod.GET,RequestMethod.POST})
    public MessageVo<Integer> getShoppingCartCount(String openId){
        try {
            if (StringUtil.isEmpty(openId)){
                return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100004.getCode(),BaseErrorCodeEnum.ERRCODE_100004.getMsg());
            }
            return baseInfoApi.getShoppingCartCount(openId);
        }catch (Exception e){
            LogUtil.error(logger,e,"getShoppingCartCount_method_error");
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100012.getCode(),BaseErrorCodeEnum.ERRCODE_100012.getMsg());
    }

    /**
     * Description:  查询购物车<br/>
     * Version V1.0  2019-8-29 11:08  by 秦涛   (qintao@cloud-young.com) 创建
     * @param openId 用户id
     * @return 列表
     */
    @RequestMapping(value = "getShoppingCartList",method={ RequestMethod.GET,RequestMethod.POST})
    public MessageVo<List<EsSpuVo>> getShoppingCartList(String openId){
        try {
            if (StringUtil.isEmpty(openId)){
                return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100004.getCode(),BaseErrorCodeEnum.ERRCODE_100004.getMsg());
            }
            return baseInfoApi.getShoppingCartList(openId);
        }catch (Exception e){
            LogUtil.error(logger,e,"getShoppingCartList_method_error");
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100013.getCode(),BaseErrorCodeEnum.ERRCODE_100013.getMsg());
    }

    /**
     * Description:  添加商品到购物车，加减商品数量<br/>
     * Version V1.0  2019-8-29 14:46  by 秦涛   (qintao@cloud-young.com) 创建
     * @param shoppingCart 购物车参数
     * @return true添加成功
     */
    @RequestMapping(value = "addGoods",method={ RequestMethod.POST})
    public MessageVo<Boolean> addGoods(@RequestBody ShoppingCart shoppingCart){
        try {
            if (Objects.isNull(shoppingCart) || Objects.isNull(shoppingCart.getSkuId()) || StringUtil.isEmpty(shoppingCart.getOpenId()) || Objects.isNull(shoppingCart.getQuantity())){
                return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100004.getCode(),BaseErrorCodeEnum.ERRCODE_100004.getMsg());
            }
            return baseInfoApi.addGoods(shoppingCart);
        }catch (Exception e){
            LogUtil.error(logger,e,"addGoods_method_error");
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100014.getCode(),BaseErrorCodeEnum.ERRCODE_100014.getMsg());
    }

    /**
     * Description:  根据类型查询商品<br/>
     * Version V1.0  2019-8-30 16:47  by 秦涛   (qintao@cloud-young.com) 创建
     * @param param 参数
     * @return 列表
     */
    @RequestMapping(value = "getEsSkuList",method={ RequestMethod.POST})
    public MessageVo<List<EsSpuVo>> getEsSkuList(@RequestBody EsSkuParam param){
        try {
            if (Objects.isNull(param) || Objects.isNull(param.getCategoryId())){
                return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100004.getCode(),BaseErrorCodeEnum.ERRCODE_100004.getMsg());
            }
            return baseInfoApi.getEsSkuList(param);
        }catch (Exception e){
            LogUtil.error(logger,e,"getEsSkuList_method_error");
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100015.getCode(),BaseErrorCodeEnum.ERRCODE_100015.getMsg());
    }

    /**
     * Description:  查询商品详情<br/>
     * Version V1.0  2019-8-30 17:01  by 秦涛   (qintao@cloud-young.com) 创建
     * @param esSkuId 商品id
     * @return 详情
     */
    @RequestMapping(value = "getEsSkuvoByEsSkuId",method={ RequestMethod.GET})
    public MessageVo<EsSpuVo> getEsSkuvoByEsSkuId(Long esSkuId){
        try {
            if (Objects.isNull(esSkuId)){
                return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100004.getCode(),BaseErrorCodeEnum.ERRCODE_100004.getMsg());
            }
            return baseInfoApi.getEsSkuvoByEsSkuId(esSkuId);
        }catch (Exception e){
            LogUtil.error(logger,e,"getEsSkuvoByEsSkuId_method_error");
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100016.getCode(),BaseErrorCodeEnum.ERRCODE_100016.getMsg());
    }

}
